package rwt.server;

import java.util.List;

import rwt.server.core.BaseMessage;
import rwt.server.mtl.*;

/**
 * Messages that pass between this web server and the railroad system (the Controller or LocoBuffer server).
 * These messages pass over a TCP connection as a sequence of bytes. The MessageAdaptor provides functions for
 * converting these messages to and from byte sequences.
 */
public class RailroadMessages
{
	//--------------------------------------------------------------------------
	// Extended messages (for the Controller)
	//--------------------------------------------------------------------------
	
	// Requests to the controller
	
	public static class DoLocoInitMsg extends BaseMessage
	{
		public int locoAddress;
		public List<Integer> sensors;
	}
	
	public static class DoReadLayoutMsg extends BaseMessage
	{
		public String fileName;
		
		public DoReadLayoutMsg() {}
		
		public DoReadLayoutMsg(String fileName)
		{
			this.fileName = fileName;
		}
	}
	
	public static class GetSwitchStatesMsg extends BaseMessage
	{
		
	}
	
	// Responses from the controller
	
	public static class PutReadLayoutResponseMsg extends BaseMessage
	{
		public int responseFlag; // 1, If successful
		public int code; // 0, if successful
	}
	
	public static class PutInitOutcomeMsg extends BaseMessage
	{
		public int physicalLocoAddress;
		public int physicalSlotNum;
		public int virtualLocoAddress;
		public int virtualSlotNum;
	}
	
	public static class PutSwitchStateMsg extends BaseMessage
	{
		public int switchId;
		public int state;
	}
	
	public static class PutTrainStateMsg extends BaseMessage
	{
		public int slot;
		public int state;
	}
	
	public static class PutTrainInformationMsg extends BaseMessage
	{
		public int slot;
		public int speed;
		public int direction;
		public int light;
		public int bell;
		public int horn;
		public int mute;
	}
	
	//--------------------------------------------------------------------------
	// LocoNet messages
	//--------------------------------------------------------------------------
	
	// Requests to the railroad/controller

	public static class PowerOnMsg extends BaseMessage
	{
		public PowerOnMsg() {}
	}

	public static class PowerOffMsg extends BaseMessage
	{
		public PowerOffMsg() {}
	}

	public static class LocoSpdMsg extends BaseMessage
	{
		public int slot;
		public int speed;
		
		public LocoSpdMsg() {}
		
		public LocoSpdMsg(int slot, int speed)
		{
			this.slot = slot;
			this.speed = speed;
		}
	}
	
	public static class LocoDirfMsg extends BaseMessage
	{
		public int slot;
		public int direction;
		public int light;
		public int horn;
		public int bell;
		
		public LocoDirfMsg() {}
		
		public LocoDirfMsg(int slot, int direction, int light, int horn, int bell)
		{
			this.slot = slot;
			this.direction = direction;
			this.light = light;
			this.horn = horn;
			this.bell = bell;
		}
	}
	
	public static class LocoSndMsg extends BaseMessage
	{
		public int slot;
		
		// F5 and F6 were added to the Ada MTL's makeLocoSndMsg method on 11/13/2012. Previously, these two
		// fields existed in splitLocoSndMsg, but not makeLocoSndMsg.
		public int F5; // Closes next switch in front of train
		public int F6; // Throws next switch in front of train
		public int mute; // Mute On (1) = no sound; Mute Off (0) = sound
		
		public LocoSndMsg() {}
		
		public LocoSndMsg(int slot, int closeNext, int throwNext, int mute)
		{
			this.slot = slot;
			this.F5 = closeNext;
			this.F6 = throwNext;
			this.mute = mute;
		}
	}
	
	public static class SwReqMsg extends BaseMessage
	{
		public int switchId;
		public int direction;
		
		public SwReqMsg() {}
		
		public SwReqMsg(int switchId, int direction)
		{
			this.switchId = switchId;
			this.direction = direction;
		}
	}

	public static class LocoAdrMsg extends BaseMessage
	{
		@MakeMethodOnlyField public int locoAddress;
		@SplitMethodOnlyField public int Address;
		
		public LocoAdrMsg() {}
		
		public LocoAdrMsg(int locoAddress)
		{
			this.locoAddress = locoAddress;
		}
	}
	
	public static class MoveSlotsMsg extends BaseMessage
	{
		@MakeMethodOnlyField public int slot1;
		@MakeMethodOnlyField public int slot2;
		@SplitMethodOnlyField public int slot;
		
		public MoveSlotsMsg() {}
		
		public MoveSlotsMsg(int slot1, int slot2)
		{
			this.slot1 = slot1;
			this.slot2 = slot2;
		}
	}
	
	public static class WriteSlotDataToClearMsg extends BaseMessage
	{
		public int slotId;
		
		public WriteSlotDataToClearMsg() {}
		
		public WriteSlotDataToClearMsg(int slotId)
		{
			this.slotId = slotId;
		}
	}
	
	// Responses from the railroad/controller
	
	public static class SwRepMsg extends BaseMessage
	{
		public int switchId;
		public int state;
	}
	
	@SplitMethodOnlyMessage
	public static class SlRdDataMsg extends BaseMessage
	{
		public int locoAddress;
		public boolean isAddressAlreadyInUse;
		public int slot;
	}
}
